home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / 3DDEMO.ZIP / 3D / SOURCE / 3DCFG.CPP next >
C/C++ Source or Header  |  1996-07-23  |  13KB  |  434 lines

  1. #include <time.h>
  2. #include <stdarg.h>
  3.  
  4. #include "defines.hpp"
  5. #include "input.hpp"
  6. #include "video.hpp"
  7. #include "mesh.hpp"
  8. #include "file.hpp"
  9.  
  10. // Copyright (c) 1996 by Kerrigan Burgess, all rights reserved.
  11.  
  12. INPUTCLASS      *Input;
  13. VIDEOCLASS      *Video;
  14. MESHCLASS       *Mesh;
  15. FILESYSTEMCLASS *File;
  16.     
  17. void Error(char *fmt, ...);        // Always put.
  18.  
  19. void Build(const int Device, int Mode);       // Your own functions.
  20. void TearDown(void);
  21. void Handle3DInput(void);
  22. void SetupCFG(char *filename);
  23.  
  24.  
  25. void main(int argc, char **argv)
  26. {
  27.    if (argc < 2)
  28.      cout<<"USAGE: 3dcfg filname.cfg\n";
  29.    else
  30.    {   
  31.  
  32.      SetupCFG(argv[1]);
  33.    
  34.      float FrameRate;
  35.      long start,end;
  36.      long count=0;
  37.     
  38.      start = clock ();
  39.      while (!Input->Is_Key(SCAN_ESC))
  40.      {
  41.  
  42.         Video->ClearBuffer();
  43.           Handle3DInput();
  44.           Mesh->_3DPipeLine();         // do the rendering pipeline.
  45.         Video->Buf2Video();
  46.  
  47.         count++;
  48.      }
  49.     end = clock ();
  50.     
  51.     TearDown();
  52.  
  53.     FrameRate = (float)(CLOCKS_PER_SEC*count/(float)(end-start));   
  54.     cout<<"Frame rate was " << FrameRate << " fps \n";
  55.    }
  56. }
  57.  
  58.  
  59. void Build(const int Device, int Mode)
  60. {
  61.     Video  = NULL;
  62.     Mesh   = NULL;
  63.     File   = NULL;
  64.     Input  = NULL;
  65.  
  66.     Video        = new VIDEOCLASS;
  67.     if (Video==NULL)
  68.       Error("Not enough memory\n");      
  69.  
  70.     switch (Device)
  71.      {
  72.        case KEYBOARD:
  73.          Input = new KEYBOARDCLASS;
  74.          if (Input==NULL)
  75.            Error("Not enough memory\n");
  76.        break;
  77.        case JOYSTICK:
  78.        break;
  79.        case MOUSE:
  80.        break;
  81.        default:
  82.        break;
  83.      }
  84.  
  85.     Video->InitVideo(Mode);                   // initialize video mode.
  86.     Video->CreateBuffer(255);                   // create double buffer, flood with white color.
  87. }
  88.  
  89. void TearDown()
  90. {
  91.     if (Input != NULL)
  92.       delete Input;   
  93.     if (Video != NULL)
  94.       delete Video;
  95.     if (Mesh != NULL)
  96.       delete Mesh;
  97.     if (File != NULL)
  98.       delete File;
  99. }
  100.  
  101. char meshfile[10][30];
  102. char String[30],MorphFlag[30],ShadowFlag[30];
  103. char TransOn[30],HazeOn[30];
  104. char Backgroundfile[30],TransTblfile[30];
  105. char HazeTblfile[30],PaletteLinearfile[30],PalettePhongfile[30],Texturefile[30];
  106. int XMinClip,YMinClip,XMaxClip,YMaxClip,YonClip,HitherClip;
  107. int VidMode,ControlMode,nummesh;
  108. int rx,ry,rz;
  109. float wx,wy,wz;
  110. float cx,cy,cz,cax,cay,caz;
  111. float lx,ly,lz;
  112.  
  113. void SetupCFG(char *filename)
  114. {
  115.   FILE *fptr;
  116.  
  117.   fptr=fopen(filename,"r");
  118.   if (fptr == NULL)
  119.     Error("File does not exist\n");
  120.  
  121.    char *type;
  122.    type=strrchr(filename,'.');          // look for filename extension.
  123.    type++;                           
  124.    
  125.    if (strncmp(type,"cfg",3) !=0 )
  126.      Error("Invalid file. File must be in .cfg format\n");
  127.  
  128.    fscanf(fptr,"DEVICES\n");
  129.    cout<<"DEVICES\n";
  130.      fscanf(fptr,"  BEGIN\n");
  131.      cout<<"  BEGIN\n";
  132.        fscanf(fptr,"    VIDEOMODE = %s\n",String);
  133.        if (strcmp(String,"MCGA_320_200_256")==0)
  134.          VidMode = MCGA_320_200_256;
  135.        else
  136.          Error("Video mode not yet implemented");
  137.        cout<<"    VIDEO MODE = "<<String<<"\n";
  138.        fscanf(fptr,"    CONTROL = %s\n",String);
  139.        if (strcmp(String,"KEYBOARD")==0)
  140.          ControlMode = KEYBOARD;
  141.        else
  142.          Error("Control mode not yet implemented");
  143.        cout<<"    CONTROL MODE = "<<String<<"\n";
  144.      fscanf(fptr,"  END\n");
  145.      cout<<"  END\n\n";
  146.    
  147.    fscanf(fptr,"ENVIROMENT\n");
  148.    cout<<"ENVIROMENT\n";
  149.      fscanf(fptr,"  BEGIN\n");
  150.      cout<<"  BEGIN\n";
  151.        fscanf(fptr,"    HITHER PLANE = %d\n",&HitherClip);
  152.          cout<<"    HITHER PLANE = "<<HitherClip<<"\n";
  153.        fscanf(fptr,"    YON PLANE = %d\n",&YonClip);
  154.          cout<<"    YON PLANE = "<<YonClip<<"\n";
  155.        fscanf(fptr,"    MINCLIPX = %d\n",&XMinClip);
  156.          cout<<"    MINCLIPX = "<<XMinClip<<"\n";
  157.        fscanf(fptr,"    MINCLIPY = %d\n",&YMinClip);
  158.          cout<<"    MINCLIPY = "<<YMinClip<<"\n";
  159.        fscanf(fptr,"    MAXCLIPX = %d\n",&XMaxClip);
  160.          cout<<"    MAXCLIPX = "<<XMaxClip<<"\n";
  161.        fscanf(fptr,"    MAXCLIPY = %d\n",&YMaxClip);
  162.          cout<<"    MAXCLIPY = "<<YMaxClip<<"\n";
  163.        fscanf(fptr,"    BACKGROUND = %s\n",Backgroundfile);
  164.          cout<<"    BACKGROUND = "<<Backgroundfile<<"\n";
  165.      fscanf(fptr,"  END\n");
  166.      cout<<"  END\n\n";
  167.  
  168.    fscanf(fptr,"MESH\n");
  169.    cout<<"MESH\n";   
  170.      fscanf(fptr,"  BEGIN\n");
  171.      cout<<"  BEGIN\n";
  172.        fscanf(fptr,"    MORPH = %s\n",MorphFlag);
  173.        cout<<"    MORPH = "<<MorphFlag<<"\n";
  174.        fscanf(fptr,"    OBJECT NUM = %d\n",&nummesh);
  175.        cout<<"    OBJECT NUM = "<<nummesh<<"\n";
  176.        int count;
  177.        for (count=0;count<nummesh;count++)
  178.        {
  179.          fscanf(fptr,"      OBJECT = %s\n",meshfile[count]);
  180.          cout<<"      OBJECT = "<<meshfile[count]<<"\n";
  181.        }
  182.        fscanf(fptr,"    ROTATION = %d %d %d\n",&rx,&ry,&rz);
  183.        cout<<"    ROTATION = "<<rx<<" "<<ry<<" "<<rz<<"\n";
  184.        fscanf(fptr,"    POSITION = %f %f %f\n",&wx,&wy,&wz);               
  185.        cout<<"    POSITION = "<<wx<<" "<<wy<<" "<<wz<<"\n";
  186.        fscanf(fptr,"    SHADOW = %s\n",ShadowFlag);
  187.        cout<<"    SHADOW = "<<ShadowFlag<<"\n";
  188.      fscanf(fptr,"  END\n");
  189.      cout<<"END\n\n";
  190.        
  191.      fscanf(fptr,"MATERIAL\n");
  192.      cout<<"MATERIAL\n";
  193.        fscanf(fptr,"  BEGIN\n");
  194.        cout<<"  BEGIN\n";
  195.          fscanf(fptr,"    TRANSPARENCY = %s\n",TransOn);
  196.          fscanf(fptr,"      TABLE = %s\n",TransTblfile);
  197.          cout<<"    TRANSPARENCY = "<<TransOn<<"\n";
  198.          cout<<"      TABLE = "<<TransTblfile<<"\n";
  199.          fscanf(fptr,"    HAZE = %s\n",HazeOn);
  200.          fscanf(fptr,"      TABLE = %s\n",HazeTblfile);
  201.          cout<<"    HAZE = "<<HazeOn<<"\n";
  202.          cout<<"      TABLE = "<<HazeTblfile<<"\n";
  203.          fscanf(fptr,"    PALETTE = %s\n",String);
  204.          fscanf(fptr,"      TABLE = %s\n",PaletteLinearfile);
  205.          cout<<"    PALETTE = "<<String<<"\n";
  206.          cout<<"      TABLE = "<<PaletteLinearfile<<"\n";
  207.          fscanf(fptr,"    PALETTE = %s\n",String);
  208.          fscanf(fptr,"      TABLE = %s\n",PalettePhongfile);
  209.          cout<<"    PALETTE = "<<String<<"\n";
  210.          cout<<"      TABLE = "<<PalettePhongfile<<"\n";
  211.          fscanf(fptr,"    TEXTURE = %s\n",Texturefile);
  212.          cout<<"    TEXTURE = "<<Texturefile<<"\n";
  213.        fscanf(fptr,"END\n");
  214.        cout<<"  END\n\n";
  215.  
  216.      fscanf(fptr,"CAMERA\n");
  217.      cout<<"CAMERA\n";
  218.        fscanf(fptr,"  BEGIN\n");
  219.        cout<<"  BEGIN\n";
  220.          fscanf(fptr,"    POSITION = %f %f %f\n",&cx,&cy,&cz);
  221.          cout<<"    POSITION = "<<cx<<" "<<cy<<" "<<cz<<"\n";
  222.          fscanf(fptr,"    ANGLE = %f %f %f\n",&cax,&cay,&caz);
  223.          cout<<"    ANGLE = "<<cax<<" "<<cay<<" "<<caz<<"\n";
  224.        fscanf(fptr,"  END\n");
  225.        cout<<"  END\n\n";
  226.  
  227.      fscanf(fptr,"LIGHT\n");
  228.      cout<<"LIGHT\n";
  229.        fscanf(fptr,"  BEGIN\n");
  230.        cout<<"  BEGIN\n";
  231.          fscanf(fptr,"    POSITION = %f %f %f\n",&lx,&ly,&lz);
  232.          cout<<"    POSITION = "<<lx<<" "<<ly<<" "<<lz<<"\n";
  233.        fscanf(fptr,"  END\n");
  234.        cout<<"  END\n\n";
  235.        
  236.        fclose(fptr);
  237.        
  238.                      // LOAD DEVICES
  239.  
  240.   Build(ControlMode,VidMode);  
  241.  
  242.                      // LOAD ENVIROMENT
  243.  
  244.   Video->SetClipExtents(XMinClip,YMinClip,XMaxClip,YMaxClip,HitherClip,YonClip);
  245.  
  246.                      // LOAD MESH
  247.                      
  248.   Mesh       = new MESHCLASS;
  249.     if (Mesh==NULL)
  250.       Error("Not enough memory\n");      
  251.  
  252.   File       = new FILESYSTEMCLASS;
  253.     if (File==NULL)
  254.       Error("Not enough memory\n");      
  255.  
  256.   int ObjectId;
  257.   ObjectId=File->CreateDataBase( Mesh,meshfile[0],PARENT );
  258.   Mesh->SetWorldXYZ( ObjectId,wx,wy,wz );
  259.   for (count=1;count<nummesh;count++)
  260.   {
  261.     ObjectId=File->CreateDataBase( Mesh,meshfile[count],CHILD );   // if object has children, load in.
  262.     Mesh->SetWorldXYZ( ObjectId,wx,wy,wz );
  263.   }
  264.   Mesh->CreateMeshList();                   // MUST HAVE:  List to hold all polygons.
  265.   Mesh->SetTSRVectors( rx,ry,rz,0,0,0 );        // start object rotating
  266.   if (strcmp(MorphFlag,"YES")==0)
  267.     Mesh->SetMorph( Morph );
  268.   else
  269.     Mesh->SetMorph( NoMorph );
  270.   if (strcmp(ShadowFlag,"YES")==0)
  271.     Mesh->SetShadow( Shadow );
  272.   else
  273.     Mesh->SetShadow( NoShadow );
  274.  
  275.                      // LOAD MATERIAL
  276.                      
  277.   if (strcmp(TransOn,"YES")==0)
  278.   {
  279.     Mesh->SetMaxTransparency( Video->LoadTransTable( TransTblfile ) );       // load in transparency table.
  280.   }    
  281.   if (strcmp(HazeOn,"YES")==0)
  282.   {
  283.     if (strcmp(TransOn,"YES")==0)
  284.       Error("Can't have both Hazing and Transparency on. Check Config file\n");
  285.     else
  286.     {
  287.       Video->LoadHazeTable( HazeTblfile );
  288.       Mesh->SetHazing( Haze );
  289.     }
  290.   }
  291.   else
  292.   if (strcmp(Backgroundfile,"NULL") != 0)         // only load if NOT hazed or if not NULL.
  293.     Video->LoadBackground( Backgroundfile );
  294.     
  295.   Video->LoadTextureMap( Texturefile );
  296.   Video->LoadPalTable( PaletteLinearfile );       // load predefined linear palette
  297.   Video->LoadPalPhongTable( PalettePhongfile );     // load predefined phong palette.
  298.   Video->LoadPhongTBL("data/phong.tbl");            // NOTE: must be loaded if you want phong shading. load precomputed angle table. Cos*NUM_SHADES.
  299.  
  300.                      // LOAD CAMERA
  301.  
  302.   Mesh->Camera[0].SetViewPoint(cx,cy,cz);
  303.   Mesh->Camera[0].SetViewAngle(cax,cay,caz);       
  304.  
  305.                      // LOAD LIGHT
  306.  
  307.   Mesh->Light[0].SetLight( lx,ly,lz);
  308. }
  309.  
  310. void Handle3DInput(void)
  311. {
  312.    int camangle_x=0,camangle_y=0,camangle_z=0;
  313.    int rotangle_x=0,rotangle_y=0,rotangle_z=0;
  314.    int transx=0,transy=0,transz=0;
  315.    float viewx=0,viewy=0,viewz=0;
  316.  
  317.   if (Input->Is_Key(SCAN_ANY))            // first see if any key is pressed!
  318.   {
  319.     if (Input->Is_Key(SCAN_F1))      // 4 virtual cameras!
  320.       Mesh->SetCurrentCamera(0);
  321.     else
  322.     if (Input->Is_Key(SCAN_F2))
  323.       Mesh->SetCurrentCamera(1);
  324.     else
  325.     if (Input->Is_Key(SCAN_F3))
  326.       Mesh->SetCurrentCamera(2);
  327.     else
  328.     if (Input->Is_Key(SCAN_F4))
  329.       Mesh->SetCurrentCamera(3);
  330.  
  331.     if (Input->Is_Key(SCAN_INSERT))       // +X
  332.       rotangle_x=1;
  333.     else
  334.     if (Input->Is_Key(SCAN_DELETE))       // -X
  335.      rotangle_x=-1;
  336.  
  337.     if (Input->Is_Key(SCAN_HOME))         // -Y
  338.       rotangle_y=-1;
  339.     else
  340.     if (Input->Is_Key(SCAN_END))          // +Y
  341.       rotangle_y=1;
  342.     
  343.     if (Input->Is_Key(SCAN_PGUP))         // +Z
  344.       rotangle_z=1;
  345.     else
  346.     if (Input->Is_Key(SCAN_PGDOWN))       // -Z
  347.      rotangle_z=-1;
  348.  
  349.     if (Input->Is_Key(SCAN_UP))           // +X
  350.       camangle_x=3;
  351.     else
  352.     if (Input->Is_Key(SCAN_DOWN))         // -X
  353.      camangle_x=-3;
  354.     
  355.     if (Input->Is_Key(SCAN_LEFT))         // -Y
  356.       camangle_y=-3;
  357.     else
  358.     if (Input->Is_Key(SCAN_RIGHT))        // +Y
  359.       camangle_y=3;
  360.     
  361.     if (Input->Is_Key(SCAN_A))
  362.       viewz=20;
  363.     else
  364.     if (Input->Is_Key(SCAN_Z))
  365.       viewz=-20;
  366.  
  367.     Mesh->SetTSRVectors(rotangle_x,rotangle_y,rotangle_z,transx,transy,transz);
  368.     Mesh->MoveCamera(camangle_x,camangle_y,camangle_z,viewx,viewy,viewz);
  369.  
  370.     if (Input->Is_Key(SCAN_W))
  371.       Mesh->SetShading(WireFrame);
  372.     if (Input->Is_Key(SCAN_C))
  373.       Mesh->SetShading(Constant);
  374.     if (Input->Is_Key(SCAN_L))
  375.       Mesh->SetShading(Lambert);     
  376.     if (Input->Is_Key(SCAN_G))
  377.       Mesh->SetShading(Gouraud);
  378.     if (Input->Is_Key(SCAN_P))
  379.       Mesh->SetShading(Phong);
  380.  
  381.     if (Input->Is_Key(SCAN_T))
  382.       Mesh->SetTextureMapping(Texture);
  383.     if (Input->Is_Key(SCAN_N))
  384.       Mesh->SetTextureMapping(NoTexture);
  385.  
  386.     if (Input->Is_Key(SCAN_MINUS))
  387.       Mesh->SetTransparencyLevel(-1);
  388.     else
  389.     if (Input->Is_Key(SCAN_EQUAL))
  390.       Mesh->SetTransparencyLevel(1);
  391.   }
  392.  
  393. }
  394.  
  395. void Error(char *fmt, ...)   
  396. {
  397.   char msg[80];
  398.   va_list argptr;
  399.  
  400.   va_start(argptr, fmt);
  401.   vsprintf(msg,fmt,argptr);
  402.   va_end(argptr);
  403.   TearDown();
  404.   cout<<msg;
  405.   exit(1);
  406. }
  407.  
  408. /*
  409.  
  410.    fscanf(fptr,"ENVIROMENT\n");
  411.    cout<<"ENVIROMENT\n";
  412.      fscanf(fptr,"  BEGIN\n");
  413.      cout<<"  BEGIN\n";
  414.        fscanf(fptr,"    HITHER PLANE = %d\n",&HITHER_Z);
  415.          cout<<"    HITHER PLANE = "<<HITHER_Z<<"\n";
  416.        fscanf(fptr,"    YON PLANE = %d\n",&YON_Z);
  417.          cout<<"    YON PLANE = "<<YON_Z<<"\n";
  418. //       fscanf(fptr,"VIEW DISTANCE = %d\n",&VIEWDISTANCE);
  419. //         cout<<"    VIEW DISTANCE = "<<VIEWDISTANCE<<"\n";
  420.        fscanf(fptr,"    MINCLIPX = %d\n",&POLY_MIN_CLIP_X);
  421.          cout<<"    MINCLIPX = "<<POLY_MIN_CLIP_X<<"\n";
  422.        fscanf(fptr,"    MINCLIPY = %d\n",&POLY_MIN_CLIP_Y);
  423.          cout<<"    MINCLIPY = "<<POLY_MIN_CLIP_Y<<"\n";
  424.        fscanf(fptr,"    MAXCLIPX = %d\n",&POLY_MAX_CLIP_X);
  425.          cout<<"    MAXCLIPX = "<<POLY_MAX_CLIP_X<<"\n";
  426.        fscanf(fptr,"    MAXCLIPY = %d\n",&POLY_MAX_CLIP_Y);
  427.          cout<<"    MAXCLIPY = "<<POLY_MAX_CLIP_Y<<"\n";
  428.        fscanf(fptr,"    BACKGROUND = %s\n",Backgroundfile);
  429.          cout<<"    BACKGROUND = "<<Backgroundfile<<"\n";
  430.      fscanf(fptr,"  END\n");
  431.      cout<<"  END\n\n";
  432. */
  433.      
  434.